const { MongoClient } = require('mongodb');
require('dotenv').config();

let db;

// 连接到数据库
const connectToDb = async () => {
  const client = new MongoClient(process.env.MONGO_URI);
  await client.connect();
  db = client.db('lowCode');
  console.log('已连接到数据库');
};

// 获取数据库连接
const getDb = () => {
  if (!db) {
    throw new Error('数据库未初始化');
  }
  return db;
};

module.exports = { connectToDb, getDb };
